﻿namespace TradePublish.Service.impl
{
    using TradePublish.Common;
    using log4net;
    using TradePublish.Common;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Reflection;
    using System.Text.RegularExpressions;
    using System.Threading;
    using TradePublish.Entity;

    internal class GraspIntelEmailManager
    {
        private string applyUserName;
        private ILog log = null;
        private string url;

        public GraspIntelEmailManager(string url, string applyUserName)
        {
            this.url = url;
            this.applyUserName = applyUserName;
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public List<ApplyUserContract> execute()
        {
            ApplyUserContract contract;
            List<ApplyUserContract> list2;
            string str6;
            ArrayList list3;
            int num;
            Hashtable hashtable = new Hashtable();
            string htmlCode = this.getHTML(this.url);
            if (htmlCode == null)
            {
                return null;
            }
            ArrayList email = Utils.GetEmail(htmlCode);
            if ((email != null) && (email.Count > 0))
            {
                foreach (string str2 in email)
                {
                    if (!(hashtable.ContainsKey(str2) || str2.Equals("hr@51job.com")))
                    {
                        contract = new ApplyUserContract {
                            applyUserName = this.applyUserName,
                            email = str2,
                            emailSourceHtml = "<a href='" + this.url + "' target='_blank'>" + this.url + "</a>"
                        };
                        hashtable.Add(str2, contract);
                    }
                }
                list2 = new List<ApplyUserContract>();
                foreach (string str3 in hashtable.Keys)
                {
                    list2.Add((ApplyUserContract) hashtable[str3]);
                }
                return list2;
            }
            Hashtable hashtable2 = this.getIndexUrl(htmlCode);
            if ((hashtable2 == null) || (hashtable2.Count == 0))
            {
                list2 = new List<ApplyUserContract>();
                contract = new ApplyUserContract {
                    applyUserName = this.applyUserName,
                    email = "",
                    emailSourceHtml = "【标记！flash】<a href='" + this.url + "' target='_blank'>官网首页" + this.url + "</a>无任何超级链接，或许是flash。"
                };
                list2.Add(contract);
                return list2;
            }
            Hashtable hashtable3 = new Hashtable();
            foreach (string str4 in hashtable2.Keys)
            {
                if ((((str4.IndexOf("联系") > 0) || (str4.Split(new char[] { ' ' })[0].IndexOf("contact") > 0)) || (str4.Split(new char[] { ' ' })[0].IndexOf("/lianxi") > 0)) && !hashtable3.ContainsKey(str4))
                {
                    hashtable3.Add(str4, str4);
                }
            }
            if (hashtable3.Count == 0)
            {
                if (hashtable2.Count > 5)
                {
                    return null;
                }
                foreach (string str5 in hashtable2.Keys)
                {
                    str6 = this.getHTML(str5.Split(new char[] { ' ' })[0]);
                    if (str6 != null)
                    {
                        list3 = Utils.GetEmail(str6);
                        if ((list3 != null) && (list3.Count > 0))
                        {
                            num = 0;
                            while (num < list3.Count)
                            {
                                if (!(hashtable.ContainsKey(list3[num]) || list3[num].Equals("hr@51job.com")))
                                {
                                    contract = new ApplyUserContract {
                                        applyUserName = this.applyUserName,
                                        email = list3[num].ToString(),
                                        emailSourceHtml = "来自首页<a href='" + this.url + "' targt='_blank'>" + this.url + "</a>中的链接<a href='" + str5.Split(new char[] { ' ' })[0] + "' targt='_blank'>" + str5.Split(new char[] { ' ' })[1] + "</a>"
                                    };
                                    hashtable.Add(list3[num], contract);
                                }
                                num++;
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (string str5 in hashtable3.Keys)
                {
                    str6 = this.getHTML(str5.Split(new char[] { ' ' })[0]);
                    if (str6 != null)
                    {
                        list3 = Utils.GetEmail(str6);
                        if ((list3 != null) && (list3.Count > 0))
                        {
                            for (num = 0; num < list3.Count; num++)
                            {
                                if (!(hashtable.ContainsKey(list3[num]) || list3[num].Equals("hr@51job.com")))
                                {
                                    contract = new ApplyUserContract {
                                        applyUserName = this.applyUserName,
                                        email = list3[num].ToString(),
                                        emailSourceHtml = "来自首页<a href='" + this.url + "' targt='_blank'>" + this.url + "</a>中的链接<a href='" + str5.Split(new char[] { ' ' })[0] + "' targt='_blank'>" + str5.Split(new char[] { ' ' })[1] + "</a>"
                                    };
                                    hashtable.Add(list3[num], contract);
                                }
                            }
                        }
                    }
                }
            }
            List<ApplyUserContract> list4 = new List<ApplyUserContract>();
            foreach (string str3 in hashtable.Keys)
            {
                list4.Add((ApplyUserContract) hashtable[str3]);
            }
            return list4;
        }

        private string getHTML(string url)
        {
            string response = null;
            int num = 0;
            while (true)
            {
                try
                {
                    response = new GetData().GetResponse(url.Split(new char[] { ' ' })[0], null);
                    if (response == null)
                    {
                        throw new Exception("访问url" + url + "，返回null.");
                    }
                    return response;
                }
                catch (Exception exception)
                {
                    num++;
                    this.log.Error(string.Concat(new object[] { "=====第", num, "次发生异常,url=", url, ".", exception.Message }));
                    Thread.Sleep(0x3e8);
                    if (num > 2)
                    {
                        return null;
                    }
                }
            }
        }

        private Hashtable getIndexUrl(string indexHTML)
        {
            Hashtable hashtable = new Hashtable();
            MatchCollection matchs = new Regex("(?is)<a[^>]*?href=(['\"]?)(?<url>[^'\"\\s>]+)\\1[^>]*>(?<text>(?:(?!</?a\\b).)*)</a>").Matches(indexHTML);
            foreach (Match match in matchs)
            {
                string url = match.Groups["url"].Value;
                string str2 = match.Groups["text"].Value;
                if (url.IndexOf("http://") < 0)
                {
                    url = this.url + url;
                }
                else if (!Utils.getHostName(url).Equals(Utils.getHostName(this.url)))
                {
                    continue;
                }
                string key = url + " " + str2;
                if (!hashtable.ContainsKey(key))
                {
                    hashtable.Add(key, key);
                }
            }
            return hashtable;
        }
    }
}

